package com.wsm.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsm.common.annotation.RedisCache;
import com.wsm.common.domain.DictDO;
import com.wsm.common.mapper.DictDao;
import com.wsm.common.service.DictService;
import com.wsm.common.utils.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class DictServiceImpl extends ServiceImpl<DictDao, DictDO> implements DictService {
    @Autowired
    private DictDao dictDao;

    @Override
    @RedisCache(value = "dict")
    public DictDO get(Long id) {
        return dictDao.get(id);
    }

    @Override
    @RedisCache(value = "dict")
    public List<DictDO> list(Map<String, Object> map) {
        return dictDao.list(map);
    }

    @Override
    @RedisCache(value = "dict")
    public int count(Map<String, Object> map) {
        return dictDao.count(map);
    }

    @Override
    @RedisCache(value = "dict",flush = true)
    public boolean save(DictDO dict) {
        dict.setId(SnowFlake.nextId());
        if (dictDao.insert(dict) > 0) {
            return true;
        }
        return false;
    }

    @Override
    @RedisCache(value = "dict",flush = true)
    public int update(DictDO dict) {
        return dictDao.orUpdate(dict);
    }

    @Override
    @RedisCache(value = "dict",flush = true)
    public int remove(Long id) {
        return dictDao.remove(id);
    }

    @Override
    @RedisCache(value = "dict",flush = true)
    public int batchRemove(Long[] ids) {
        return dictDao.batchRemove(ids);
    }

    @Override
    @RedisCache(value = "dict",flush = true)
    public List<DictDO> listType() {
        return dictDao.listType();
    }

    @Override
    @RedisCache(value = "dict",flush = true)
    public String getName(String type, String value) {
        Map<String, Object> param = new HashMap<String, Object>(16);
        param.put("type", type);
        param.put("value", value);
        List<DictDO> list = dictDao.list(param);
        if (null == list || list.size() == 0) {
            return null;
        }
        return list.get(0).getName();
    }

    @Override
    @RedisCache(value = "dict",flush = true)
    public List<DictDO> getSexList() {
        Map<String, Object> param = new HashMap<>(16);
        param.put("type", "sex");
        return dictDao.list(param);
    }

    @Override
    @RedisCache(value = "dict",flush = true)
    public List<DictDO> listByType(String type) {
        Map<String, Object> param = new HashMap<>(16);
        param.put("type", type);
        return dictDao.list(param);
    }

    @Override
    @RedisCache(value = "dict",flush = true)
    public DictDO getTypeDO(String type, String value) {
        Map<String, Object> param = new HashMap<String, Object>(16);
        param.put("type", type);
        param.put("value", value);
        List<DictDO> list = dictDao.list(param);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

}
